import { createWebHistory, createRouter } from "vue-router";//全局前置守卫
import routes from '@/router/routes.js';

const router = createRouter({
    history: createWebHistory(),
    routes: routes
});

router.beforeEach(async (to, from, next) => {
    const requireAuth = to.matched.some(record => record.meta?.requireAuth);
    const userLoggedIn = localStorage.getItem('userToken');

    if (requireAuth && !userLoggedIn) {
        next('/login');
    } else {
        next();
    }
});

export default router;